<?php
namespace app\backend\controller;

use app\backend\model\Brand as BrandModel;
use think\Db;
use think\Request;
use app\backend\org\Image;


class Brand extends Base
{
    public function index($type_id=7){
        $type_id = Request::instance()->param("type_id");
        $model = new BrandModel;
        $result = $model->getAll()->paginate(5);
        $page = $result->render();
        $this->assign('page',$page);
        $this->assign('brand',$result);
        $this->assign('empty',"暂无数据");
        $this->assign('jsId',$type_id);
        $this->assign('jsController','index');
        return $this->fetch();
        //return $this->redirect("modules/index");
    }

    public function brand_create(){
        if (Request::instance()->isPost())
        {
            $model = new BrandModel;
            $post = Request::instance()->post();
            $path = HOME_PATH;
            if(!(Image::image($post,$path))){
                $this->error("图片上传失败");
            }
            //var_dump($post);exit;
            if($post["img_type"]==1){
                $post["img_url_cn"]=$post["s_img_url"] ;
            }elseif($post["img_type"]==2){
                $post["img_url_en"]=$post["b_img_url"];
            }else{
                $post["img_url_cn"]=$post["s_img_url"] ;
                $post["img_url_en"]=$post["b_img_url"];
            }


            //var_dump($post);exit;
            unset($post["img_type"]);// 获取经过过滤的全部post变量
            unset($post["s_img_url"]);// 获取经过过滤的全部post变量
            unset($post["b_img_url"]);// 获取经过过滤的全部post变量
            //var_dump($post);exit;
            // die();
            // 判断已有内容不能重复新增去修改
            $name = Db::query("select brand_id,brand_name_cn from ls_brand where `brand_name_cn`='".$post['brand_name_cn']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return $this->redirect(url('brand_update',array('id'=>$name[0]['brand_id'])));
            }

            // 验证表单并插入数据库
            $result = $this->validate($post,'Brand.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');
            //$model->insert($post);

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('brand/index');
            }
        };
        return $this->fetch();
    }

    public function brand_update($id){
        if (Request::instance()->isPost())
        {
            $model = new BrandModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
//var_dump($post);exit;
            $path = HOME_PATH;
            if(!(Image::image($post,$path))){
                $this->error("图片上传失败");
            }
            //var_dump($post);
            //exit;
            if($post["img_type"]==1){
                $post["img_url_cn"]=$post["s_img_url"] ;
            }elseif($post["img_type"]==2){
                $post["img_url_en"]=$post["b_img_url"];
            }else{
                $post["img_url_cn"]=$post["s_img_url"] ;
                $post["img_url_en"]=$post["b_img_url"];
            }


            //var_dump($post);exit;
            unset($post["img_type"]);// 获取经过过滤的全部post变量
            unset($post["s_img_url"]);// 获取经过过滤的全部post变量
            unset($post["b_img_url"]);// 获取经过过滤的全部post变量
            //print_r($post);exit;
            //验证表单并插入数据库
            $result = $this->validate($post,'Brand.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['brand_id'=>$id]);
            //echo $result;exit;
            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->redirect('brand/index');
            }
        }
        //echo 111;
        //根据主键插记录
        $record = BrandModel::get($id);
        //var_dump($record);exit;
        $this->assign('id',$id);
        $this->assign('brand', $record);
        return $this->fetch();
    }

    public function brand_delete($id)
    {
        BrandModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect('brand/index');
        return $this->fetch();
    }
}